/**
 * 
 */
package dt.inlee.system.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dt.inlee.common.dao.impl.BaseDao;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.SysOrganization;
import dt.inlee.domain.SysUser;

/**
 * @功能说明：组织机构数据访问层
 * @作者： 印鲜刚
 * @创建日期： 2010-5-6 @
 */
@Repository("sysOrganizationDao")
public class SysOrganizationDao extends BaseDao {

	/**
	 * 通过组织机构parentId获取其所有直接子组织机构集合
	 * 
	 * @param parentId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysOrganization> findDirectSubSysOrganizations(String parentId) throws FrameworkException {
		StringBuffer hql = new StringBuffer();
		hql.append("FROM SysOrganization so ");
		if (!BaseUtil.isEmpty(parentId)) {
			hql.append("WHERE so.sysOrganization.id = ? ");
			hql.append("ORDER BY so.id,so.orderNo ASC ");
			return this.hibernateTemplate.find(hql.toString(), parentId);
		} else {
			hql.append("ORDER BY so.id,so.orderNo ASC ");
			return this.hibernateTemplate.find(hql.toString());
		}
	}

	@SuppressWarnings("unchecked")
	public List<SysOrganization> findDirectSubSysOrganizationsEx(String parentId) throws FrameworkException {
		StringBuffer hql = new StringBuffer();
		hql.append("FROM SysOrganization so ");
		if (!BaseUtil.isEmpty(parentId)) {
			hql.append("WHERE so.sysOrganization.id = ? ");
			hql.append("ORDER BY so.shortName ASC ");
			return this.hibernateTemplate.find(hql.toString(), parentId);
		} else {
			hql.append("ORDER BY so.shortName ASC ");
			return this.hibernateTemplate.find(hql.toString());
		}
	}

	/**
	 * 根据条件查询组织机构实体
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysOrganization> findAllSysOrganizations() throws FrameworkException {
		return this.findAll(SysOrganization.class);
	}

	/**
	 * 根据条件查询组织机构实体
	 * 
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SysOrganization findSysOrganizationByName(String name) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				SysOrganization.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));
		}
		List<SysOrganization> orgs = criteria.list();
		if (!BaseUtil.isEmpty(orgs) && orgs.size() > 0)
			return orgs.get(0);
		return null;
	}

	/**
	 * 子节点顺序号 批量更新
	 * 
	 * @param List
	 * @throws FrameworkException
	 */
	public void updateAll(List<SysOrganization> list, Integer value) throws FrameworkException {
		for (SysOrganization sysOrganization : list) {
			sysOrganization.setOrderNo(sysOrganization.getOrderNo() + value);
			this.update(sysOrganization);
		}
	}

	/**
	 * 获取指定节点的子节点
	 * 
	 * @param nodeId
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<SysOrganization> getSubAllNode(String nodeId) throws FrameworkException {
		String hql = "FROM SysOrganization so WHERE so.sysOrganization.id=?";
		return this.hibernateTemplate.find(hql, nodeId);
	}

	/**
	 * 根据组织机构ID及名字查找组织机构
	 * @param id
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysOrganization findSysOrganizationByIdAndName(String id, String name) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				SysOrganization.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));
		}
		criteria.add(Restrictions.ne("id", id));
		List<SysOrganization> orgs = criteria.list();
		if (!BaseUtil.isEmpty(orgs) && orgs.size() > 0)
			return orgs.get(0);
		return null;
	}

	/**
	 * 根据简称获取组织机构
	 * @param shortName
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysOrganization findSysOrganiztionByShortName(String shortName) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				SysOrganization.class);
		if (!BaseUtil.isEmpty(shortName)) {
			criteria.add(Restrictions.eq("shortName", shortName));
		}
		List<SysOrganization> orgs = criteria.list();
		if (!BaseUtil.isEmpty(orgs) && orgs.size() > 0)
			return orgs.get(0);
		return null;
	}

	/**
	 * 通过组织机构ID计算该组织机构人员数
	 * @param orgId
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public int countSysUsersByOrgId(String orgId) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(SysUser.class);
		criteria.add(Restrictions.eq("isOperator", true));
		criteria.add(Restrictions.eq("disabled", true));
		if (!BaseUtil.isEmpty(orgId)) {
			criteria.createCriteria("sysOrganizations").add(Restrictions.eq("id", orgId));
		}
		criteria.setProjection(Projections.countDistinct("id"));
		List<Integer> count = criteria.list();
		return count.get(0).intValue();
	}

	/**
	 * 通过组织机构ID获取该组织机构人员信息
	 * @param orgId
	 * @param curPage
	 * @param pageSize
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<SysUser> findSysUsersBySysOrganizationId(String orgId, int curPage, int pageSize)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(SysUser.class);
		criteria.add(Restrictions.eq("isOperator", true));
		criteria.add(Restrictions.eq("disabled", true));

		if (!BaseUtil.isEmpty(orgId)) {
			criteria.createCriteria("sysOrganizations").add(Restrictions.eq("id", orgId));
		}
		if (curPage > 0 && pageSize > 0) {
			criteria.setFirstResult((curPage - 1) * pageSize);
			criteria.setMaxResults(pageSize);
		}
		return criteria.list();
	}

}
